package com.example.app.components


import android.Manifest
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionStatus
import com.google.accompanist.permissions.rememberPermissionState
import com.google.accompanist.permissions.shouldShowRationale

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionSample() {
    // 权限状态
    // permission参数为当前想要获取的状态
    // Manifest导包需要到android的包
    // Manifest.permission.CAMERA获取相机权限
    var permissionState = rememberPermissionState(permission = Manifest.permission.CAMERA)
    Scaffold(topBar = {
        TopAppBar() {
            Text(text = "topBar")
        }
    }) {
        Column(
            Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 判断授权状态
            when(permissionState.status) {
                PermissionStatus.Granted -> {
                    Text(text = "同意授权")
                }
                // is 代表当前判断值是Denied时
                is PermissionStatus.Denied -> {
                    Column {
                        val text = if(permissionState.status.shouldShowRationale) {
                            "拒绝授权"
                        } else {
                            "同意授权"
                        }
                        Text(text = text)
                        Button(onClick = {
                            // 请求获取权限
                            // 需要在AndroidManifest中添加权限声明
                            permissionState.launchPermissionRequest()
                        }) {
                            Text(text = "点击获取权限")
                        }   
                    }
                }
            }
        }
    }
}

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionSample1() {
    // 权限状态
    // permission参数为当前想要获取的状态
    // Manifest导包需要到android的包
    // Manifest.permission.CAMERA获取相机权限
    var permissionState = rememberPermissionState(permission = Manifest.permission.ACCESS_FINE_LOCATION)
    // 后台定位权限
    var locationPromission = rememberPermissionState(permission = Manifest.permission.ACCESS_BACKGROUND_LOCATION)
    Scaffold(topBar = {
        TopAppBar() {
            Text(text = "topBar")
        }
    }) {
        Column(
            Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 判断授权状态
            when(permissionState.status) {
                PermissionStatus.Granted -> {
                    Button(onClick = {
                        locationPromission.launchPermissionRequest()
                    }) {
                        Text(text = "前台定位已就绪,点击获取后台定位权限")
                    }
                }
                // is 代表当前判断值是Denied时
                is PermissionStatus.Denied -> {
                    Column {
                        val text = if(permissionState.status.shouldShowRationale) {
                            "定位拒绝授权"
                        } else {
                            "定位同意授权"
                        }
                        Text(text = text)

                    }
                }
            }

            when(locationPromission.status) {
                PermissionStatus.Granted -> {
                    Text(text = "后台定位权限已同意")
                }
                is PermissionStatus.Denied -> {
                    Column {
                        val text = if(permissionState.status.shouldShowRationale) {
                            "定位拒绝授权"
                        } else {
                            "定位同意授权"
                        }
                        Text(text = text)

                    }
                }
            }

            // 获取前台权限
            Button(onClick = {
                // 请求获取权限
                // 需要在AndroidManifest中添加权限声明
                permissionState.launchPermissionRequest()
            }) {
                Text(text = "点击获取权限")
            }
        }
    }
}
@Preview
@Composable
fun PermissionSamplePreview() {
    PermissionSample1()
}

